#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
struct ListNode {
    int val;
    struct ListNode* next;
};
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if (list1 == NULL)
        return list2;
    if (list2 == NULL)
        return list1;
    struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* newnode = head;
    while (list1 != NULL && list2 != NULL)
    {
        if (list1->val < list2->val)
        {
            newnode = list1;
            list1 = list1->next;
            newnode = newnode->next;
        }
        else
        {
            newnode = list2;
            list2 = list2->next;
            newnode = newnode->next;
        }
    }
    if (list1 == NULL)
        newnode = list2;
    if (list2 == NULL)
        newnode = list1;
    return head;
}
int main()
{
    struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    assert(n1);
    n1->val = 1;
    n1->next->val = 3;
    n1->next->next->val = 5;
    struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    assert(n2);
    n2->val = 2;
    n2->next->val = 4;
    struct ListNode* tmp = mergeTwoLists(n1, n2);
    for (int i = 0; i < 4; i++)
    {
        printf("%d ", tmp[i]);
    }
    return 0;
}